 
  

 






<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>

<!-- Mirrored from www.javapractices.com/topic/TopicAction.do;jsessionid=4FCCB481C702D708A7360133D128E359?Id=228 by HTTrack Website Copier/3.x [XR&CO'2010], Sun, 12 Jun 2011 17:27:10 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=UTF-8"><!-- /Added by HTTrack -->
<head>
 <title>
  Java Practices -> Don't subclass JDialog or JFrame
 </title>
 <link rel="stylesheet" type="text/css" href="../stylesheet8.css" media="all">
 
 <link rel="shortcut icon" href='../images/favicon.ico' type="image/vnd.microsoft.icon">
 <meta name="description" content="Concise presentations of java programming practices, tasks, and conventions, amply illustrated with syntax highlighted code examples.">
 
 <meta name='keywords' content='java,java programming,java practices,java idiom,java style,java design patterns,java coding conventions,'>
 
 
</head>
 
<body>


<div class='menu-bar'>
 
  <a href='../home/HomeAction.html' title='Table of Contents'>Home</a> |
  <a href='../vote/VoteSummaryAction-2.html' title='View Poll Results'>Poll</a> |
   
  <A href='../feedback/FeedbackAction451f-2.html?Operation=Show' title='Send Your Feedback'>Wiki</a> |
  <b><a href='../source/SourceAction-2.html' title='Grab Source Code'>Source Code</a></b><IMG class='no-margin' SRC="../images/goldstar.gif" ALT=""> |

  <a href='http://www.web4j.com/Java_Web_Application_Framework_Overview.jsp?From=1' title='Free Download - Java Web Application Framework'><b>WEB4J</b></a> |
  
  <a href='http://www.date4j.net/' title='Replacement for java.util.Date'><b>DATE4J</b></a> |

   <a href='../references/ReferencesAction-2.html' title='References'>Links</a>
   
  <form action='http://www.javapractices.com/search/SearchAction.do' method='get' class='search-form'>
   <input type='text' name='SearchTerms' value="" size=12 maxlength=50 class='search'>
   <input type='submit' value="Search">
  </form>
 
</div>

<P>



  

 






<p class="display-messages">

 

 

</p>


<div class="main-layout">
 
   

 




<div class='page-title'>Don't subclass JDialog or JFrame</div>

<div class='main-body'>
 
<br>When creating a class based on <tt>JDialog</tt> or <tt>JFrame</tt>, there are two choices :

<ul>
 <li>either subclass the JDK class using <tt>extends</tt>
 <li>define a field (instance variable) to hold the <tt>JDialog</tt> or <tt>JFrame</tt>
</ul>

The second method has a nice advantage: your class's javadoc will not be cluttered up with items that are usually irrelevant. 
The Java Swing API is very extensive, and it's common for its javadoc to be a bit overwhelming. 
Using <tt>JDialog</tt> and <tt>JFrame</tt> as a <em>field</em>, and not as a base class, will automatically remove all such extraneous methods from your own class's javadoc.

<P>Of course, sometimes the caller will want access to the underlying <tt>JDialog</tt> or <tt>JFrame</tt>.
In that case, just return the needed object using a <tt>getXXX()</tt> method.

<P><b>Example</b>
<P>Here is a GUI class which does not extend any base class. 
In addition, it uses a <a href='TopicAction7910-2.html?Id=150'>standard dialog</a> class.
<br>
<PRE>

<span class='keyword'>package</span> hirondelle.movies.edit;

<span class='keyword'>import</span> hirondelle.movies.util.ui.OnClose;
<span class='keyword'>import</span> hirondelle.movies.util.Edit;
<span class='keyword'>import</span> hirondelle.movies.util.Util;
<span class='keyword'>import</span> hirondelle.movies.util.ui.UiUtil;
<span class='keyword'>import</span> hirondelle.movies.util.ui.StandardDialog;

<span class='keyword'>import</span> java.util.logging.Logger;
<span class='keyword'>import</span> java.util.*;
<span class='keyword'>import</span> java.awt.event.ActionEvent;
<span class='keyword'>import</span> java.awt.event.ActionListener;
<span class='keyword'>import</span> javax.swing.*;

<span class='comment'>/**
  Dialog allowing user to input {@link Movie} information.

  &lt;P&gt;This view can be used to either add a new Movie, or to change an existing one.
  &lt;P&gt;It's important to note that validation of user entries is &lt;em&gt;not&lt;/em&gt; 
  performed by this class. Rather, it's performed by the {@link Movie} class.
*/</span>
<span class='keyword'>final</span> <span class='keyword'>class</span> MovieView {

  <span class='comment'>/**
   Constructor.
    
   &lt;P&gt;Called when adding a new {@link Movie}.
  */</span>
  MovieView(JFrame aParent) {
    fEdit = Edit.ADD;
    buildGui(aParent, <span class='literal'>"Add Movie"</span>);
    fStandardDialog.display();
  }

  <span class='comment'>/**
    Constructor.
    
    &lt;P&gt;Called when editing an existing {@link Movie}. The text fields are simply
    prepopulated with the text taken from the currently selected row of the table.
  */</span>
  MovieView(JFrame aParent, Movie aSelectedMovie) {
    fLogger.fine(<span class='literal'>"Editing selected movie:"</span> + aSelectedMovie);
    fEdit = Edit.CHANGE;
    fId = aSelectedMovie.getId();
    buildGui(aParent, <span class='literal'>"Edit Movie"</span>);
    populateFields(aSelectedMovie);
    fStandardDialog.display();
  }

  <span class='comment'>/**
   Return the movie id. The id is used by the database, but is never shown to the user,
   nor is it ever edited by the end user. This method is supplied since it's
   convenient to carry the id with the other information related to a movie, and the
   {@link MovieDAO} needs a way to uniquely identify records.
  */</span>
  String getId() {
    <span class='keyword'>return</span> fId;
  }

  <span class='comment'>/** The title of the movie, as entered by the user. */</span>
  String getTitle() {
    <span class='keyword'>return</span> fTitle.getText();
  }

  <span class='comment'>/** The date the movie was viewed, as entered by the user. */</span>
  String getDateViewed() {
    <span class='keyword'>return</span> fDateViewed.getText();
  }

  <span class='comment'>/** The movie rating, as entered by the user. */</span>
  String getRating() {
    <span class='keyword'>return</span> fRating.getText();
  }

  <span class='comment'>/** The comment on the movie, as entered by the user. */</span>
  String getComment() {
    <span class='keyword'>return</span> fComment.getText();
  }

  <span class='comment'>/** Close the view. */</span>
  <span class='keyword'>void</span> closeDialog() {
    fStandardDialog.dispose();
  }

  <span class='comment'>/** Return the underlying dialog. */</span>
  JDialog getDialog() {
    <span class='keyword'>return</span> fStandardDialog.getDialog();
  }

  <span class='comment'>// PRIVATE //
</span>  <span class='keyword'>private</span> StandardDialog fStandardDialog;
  <span class='keyword'>private</span> Edit fEdit;
  <span class='keyword'>private</span> String fId;
  <span class='keyword'>private</span> JTextField fTitle = <span class='keyword'>new</span> JTextField();
  <span class='keyword'>private</span> JTextField fDateViewed = <span class='keyword'>new</span> JTextField();
  <span class='keyword'>private</span> JTextField fRating = <span class='keyword'>new</span> JTextField();
  <span class='keyword'>private</span> JTextField fComment = <span class='keyword'>new</span> JTextField();
  <span class='keyword'>private</span> JButton fEditButton;
  <span class='keyword'>private</span> <span class='keyword'>static</span> <span class='keyword'>final</span> Logger fLogger = Util.getLogger(MovieView.<span class='keyword'>class</span>);

  <span class='comment'>/** Populate the GUI with data from the movie. */</span>
  <span class='keyword'>private</span> <span class='keyword'>void</span> populateFields(Movie aSelectedMovie) {
    fTitle.setText(Util.format(aSelectedMovie.getTitle()));
    fDateViewed.setText(Util.format(aSelectedMovie.getDateViewed()));
    fRating.setText(Util.format(aSelectedMovie.getRating()));
    fComment.setText(aSelectedMovie.getComment());
  }

  <span class='keyword'>private</span> <span class='keyword'>void</span> buildGui(JFrame aParent, String aDialogTitle) {
    fStandardDialog = <span class='keyword'>new</span> StandardDialog(
      aParent, aDialogTitle, <span class='keyword'>true</span>, OnClose.DISPOSE, getUserInputArea(), getButtons()
    );
    fStandardDialog.setDefaultButton(fEditButton);
  }

  <span class='keyword'>private</span> JPanel getUserInputArea() {
    JPanel result = <span class='keyword'>new</span> JPanel();
    result.setLayout(<span class='keyword'>new</span> BoxLayout(result, BoxLayout.Y_AXIS));

    addTextField(fTitle, <span class='literal'>"Title"</span>, result);
    addTextField(fDateViewed, <span class='literal'>"Date Viewed"</span>, result);
    addTextField(fRating, <span class='literal'>"Rating"</span>, result);
    addTextField(fComment, <span class='literal'>"Comment"</span>, result);
    UiUtil.alignAllX(result, UiUtil.AlignX.LEFT);
    <span class='keyword'>return</span> result;
  }

  <span class='keyword'>private</span> <span class='keyword'>void</span> addTextField(JTextField aTextField, String aLabel, JPanel aPanel) {
    JLabel label = <span class='keyword'>new</span> JLabel(aLabel);
    aPanel.add(label);
    aPanel.add(aTextField);
    aTextField.setColumns(<span class='literal'>15</span>);
  }

  <span class='keyword'>private</span> java.util.List&lt;JButton&gt; getButtons() {
    java.util.List&lt;JButton&gt; result = <span class='keyword'>new</span> ArrayList&lt;JButton&gt;();

    fEditButton = <span class='keyword'>new</span> JButton(fEdit.toString());
    fEditButton.addActionListener(<span class='keyword'>new</span> MovieController(<span class='keyword'>this</span>, fEdit));
    result.add(fEditButton);

    JButton cancel = <span class='keyword'>new</span> JButton(<span class='literal'>"Cancel"</span>);
    cancel.addActionListener(<span class='keyword'>new</span> ActionListener() {
      <span class='keyword'>public</span> <span class='keyword'>void</span> actionPerformed(ActionEvent arg0) {
        closeDialog();
      }
    });
    result.add(cancel);
    <span class='keyword'>return</span> result;
  }
}
 
</PRE>
<br>
<br>
<br>

</div>




<div class='topic-section'>See Also :</div>
<div class='main-body'>
 
  
  <a href='TopicAction7910-2.html?Id=150'>Standardized dialogs</a> <br>
 
</div>


<div class='topic-section'>Would you use this technique?</div>
<div class='main-body'>
  
  <form action="http://www.javapractices.com/vote/AddVoteAction.do" method='post'>
    Yes<input type='radio' name='Choice' value='Y' >
    &nbsp;&nbsp;No<input type='radio' name='Choice' value='N'>
    &nbsp;&nbsp;Undecided<input type='radio' name='Choice' value="?" >
    &nbsp;&nbsp;<input type=submit value="Vote" >
    <input type='hidden' name='Operation' value='Apply'>
    <input type='hidden' name='TopicId' value='228'>
  </form>
</div>

<div style='height:10.0em;'></div>

 
 
</div>

  

 





<div align='center' class='legalese'>  
&copy; 2011 Hirondelle Systems |
<a href='../source/SourceAction-2.html'><b>Source Code</b></a><IMG class='no-margin' SRC="../images/goldstar.gif" ALT=""> |
<a href="mailto:webmaster@javapractices.com">Contact</a> |
<a href="http://creativecommons.org/licenses/by-nc-sa/1.0/">License</a> |
<a href='../apps/cjp.rss'>RSS</a>
<!-- ukey="2AC36CD2" -->
<!-- ckey="16DF3D87" -->
<br>

 Individual code snippets can be used under this <a href='../LICENSE.txt'>BSD license</a> - Last updated on June 6, 2010.<br>
 Over 150,000 unique IPs last month - <span title='Java Practices 2.6.5, Mon May 16 00:00:00 EDT 2011'>Built with</span> <a href='http://www.web4j.com/'>WEB4J</a>.<br>
 - In Memoriam : Bill Dirani -
</div>

<script src="../../www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2633428-1";
urchinTracker();
</script>



</body>

<!-- Mirrored from www.javapractices.com/topic/TopicAction.do;jsessionid=4FCCB481C702D708A7360133D128E359?Id=228 by HTTrack Website Copier/3.x [XR&CO'2010], Sun, 12 Jun 2011 17:27:10 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=UTF-8"><!-- /Added by HTTrack -->
</html>
